<template>
  <el-dialog title="编辑人员" :visible.sync="isEditPersonnel" width="40%" :before-close="handleClose" :close-on-click-modal='false'>
    <el-form ref="form" :model="form" label-width="80px" label-position="top" size="small" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名：" prop="full_name">
            <el-input v-model="form.full_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别：" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择性别">
              <el-option v-for="sexOptions in this.sexOptions" :key="sexOptions.id" :label="sexOptions.value" :value="sexOptions.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账号：" prop="username">
            <el-input v-model="form.username" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码：">
            <el-col :span="18" :xl="{span: 20}">
              <el-input type="password" v-model="form.password"></el-input>
            </el-col>
            <el-col :span="3" :xl="{span: 4}">
              <el-button type="text" @click="resetPassWord">重置密码</el-button>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="手机号：">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色：">
            <el-select v-model="form.role_id" placeholder="请选择角色" @change="change()">
              <el-option v-for="roleOptions in this.roleOptions" :key="roleOptions.id" :label="roleOptions.role_name" :value="roleOptions.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="状态：">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option v-for="statusOptions in this.statusOptions" :key="statusOptions.id" :label="statusOptions.value" :value="statusOptions.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button type="primary" @click="checkFn()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {APIMEMBERGETINFO, APIROLELIST, APIMEMBEREDIT} from '@/common/api.js'
export default {
  props: ['isEditPersonnel', 'row'],
  data () {
    return {
      form: {
        full_name: '',
        sex: '',
        username: '',
        password: '',
        mobile: '',
        email: '',
        role_id: '',
        status: ''
      },
      rules: {
        full_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ]
      },
      roleOptions: [],
      statusOptions: [
        {id: 1, value: '启用'},
        {id: 0, value: '禁用'}
      ],
      sexOptions: [
        {id: 1, value: '男'},
        {id: 2, value: '女'}
      ]
    }
  },
  created () {
    this.roleList()
    this.getEdit()
  },
  methods: {
    getEdit () {
      APIMEMBERGETINFO({
        uid: this.row.id
      }).then((res) => {
        // this.form = res.data
        this.form.password = ''
        this.form.full_name = res.data.full_name
        this.form.sex = res.data.sex
        this.form.username = res.data.username
        this.form.mobile = res.data.mobile
        this.form.email = res.data.email
        this.form.role_id = res.data.role_id
        this.form.status = res.data.status
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    roleList () {
      APIROLELIST().then((res) => {
        this.roleOptions = res.data.list
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    handleClose () {
      this.$emit('noEditPersonnel')
    },
    close (type) {
      this.$emit('noEditPersonnel', type)
    },
    resetPassWord () {
      this.form.password = '@Mindone123456'
    },
    checkFn () {
      if (this.form.full_name === '') {
        this.$message.warning('姓名不能为空')
        return false
      }
      if (this.form.sex === '') {
        this.$message.warning('性别不能为空')
        return false
      }
      if (this.form.username === '') {
        this.$message.warning('账号不能为空')
        return false
      }
      this.submit()
    },
    change () {
      this.$forceUpdate() // 强渲染
    },
    submit () { // 提交
      APIMEMBEREDIT({
        uid: this.row.id,
        full_name: this.form.full_name,
        username: this.form.username,
        password: this.form.password,
        mobile: this.form.mobile,
        role_id: this.form.role_id,
        email: this.form.email,
        sex: this.form.sex,
        status: this.form.status
      }).then((res) => {
        this.$message.success(res.msg)
        this.close(1)
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    }
  }
}
</script>
